import HDataTable from "@/components/ant-ext-table/HDataTable";
import SysRole from "@/api/sys-role";
import HTag from "@/components/ant-ext/HTag";
import {Stat} from "@/const/const";
import {Form} from "antd";
import RoleEdit from "@/pages/sys/role/edit";
import {DeleteOutlined, PlusSquareOutlined} from "@ant-design/icons";
import RoleMenu from "@/pages/sys/role/roleMenu";
import HAction from "@/components/common/HAction";
import React from "react";
import {HDataTableSlice} from "@/components/ant-ext-table/redux/dataTableSlice";
import {Dispatch} from "redux";
import {HTableProps} from "@/components/ant-ext-table/HTable";
import {useDispatch} from "react-redux";
import SysAdmin from "@/api/sys-admin";

function getColumns(dispatch: Dispatch): HTableProps<any>["columns"] {
    return [
        {
            title: "角色编码",
            dataIndex: 'roleCode',
            width: '200px',
            fixed: 'left',
            render: (value, record) => {
                return <HAction type={"link"}
                                empower={SysAdmin.EDIT}
                                invalid
                                action={() => dispatch!(HDataTableSlice.actions.doAction({
                                    action: SysRole.EDIT,
                                    rowKey: SysRole.RowKey,
                                    row: record
                                }))}
                                children={value}/>
            }
        }, {
            title: "角色名称",
            dataIndex: 'roleName',
        }, {
            title: "状态",
            dataIndex: 'stat',
            render: (value: any) => <HTag value={value} mapping={Stat.tag}/>
        }, {
            title: "隶属组织",
            dataIndex: 'orgNamePath',
        }, {
            title: "关联人员",
            dataIndex: 'adminNumber',
        }
    ]
}

export default function Index() {
    const [form] = Form.useForm()
    const dispatch = useDispatch()

    return <>
        <RoleEdit/>
        <RoleMenu/>
        <HDataTable
            option={[
                {key: SysRole.ADD, title: "新增", icon: <PlusSquareOutlined/>},
                {
                    key: SysRole.DEL,
                    title: "删除",
                    mode: "multi",
                    icon: <DeleteOutlined style={{color: "red"}}/>,
                    confirm: "是否要删除所选数据",
                    action: (data) => {
                        SysRole.delete({
                            body: {code: data.selectRowKeys},
                            success: () => dispatch(HDataTableSlice.actions.setQueryParams({}))
                        })
                    }
                },
                {key: SysRole.ROLE_MENU, title: "分配权限", mode: "only"},
            ]}
            queryForm={{
                form: form,
                item: [
                    {name: "roleName", label: "角色名称"},
                    {name: "stat", label: "状态", type: "select", dataSource: Stat.select, width: "150px"},
                ]
            }}
            columns={getColumns(dispatch)} selectMethod={SysRole.selectTablePage}
            rowKey={"roleCode"}
        />
    </>
}